<template>
	<view class="ad-container">
		<!-- #ifdef MP-WEIXIN -->
		<view class="banner-ad m-t-10" :class="fixed ? 'fixed' : ''">
			<!-- 信息流广告位 -->
			<ad v-if="type === 1 && config.banner_ad_id" :unit-id="config.banner_ad_id" ad-intervals="30"></ad>
			<!-- 视频广告 -->
			<ad v-if="type === 2 && config.video_ad_id" :unit-id="config.video_ad_id" ad-type="video" ad-theme="white"></ad>
			<!-- 原生广告 -->
			<ad-custom v-if="type === 3 && config.custom_ad_id" :unit-id="config.custom_ad_id" ad-intervals="30"></ad-custom>
		</view>
		<!-- #endif -->
	</view>
</template>

<script>
	export default {
		name:"ad",
		props: {
			page: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				type: 1,
				fixed: false,
				config: {}
			};
		},
		mounted() {
			let config =  this.$cache.get('ad_config')
			if(config){
				this.type = this.page === 'upload' ? 2 : Math.floor(Math.random() * 3 + 1)
				this.config = JSON.parse(config)
				this.fixed = this.config.ad_fixed === '1' ? true : false
			}
		},
	}
</script>

<style lang="scss" scoped>
	.banner-ad{
		left: 0;
		bottom: 0;
		width: 100%;
	}
	.fixed{
		position: fixed;
	}
</style>